package hxy.bytecode;

import java.util.Arrays;

/**
 * @author eric
 * @program bytecode
 * @description 给定一个整数数组，把0挪到最后，其他数字的顺序不变。要求时间复杂度为1. 两两交换
 * @date 2021/6/27
 */
public class ArrayMove {
    public static void main(String[] args) {
        int[] array = new int[]{1, 2, 0, 4, 5, 0, 6, 7, 9};
        int index2;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == 0) {
                // 查找下一个不为0的数组下标交换下。
                index2 = i;
                if (index2 < array.length - 1) {
                    while (array[index2] == 0) {
                        index2++;
                        if (index2 >= array.length) {
                            break;
                        }
                    }
                    if (index2 <= array.length - 1) {
                        int temp = array[index2];
                        array[index2] = array[i];
                        array[i] = temp;
                    }
                }
                index2++;
            }
        }
        final int[] ints = Arrays.stream(array).toArray();
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i]);
        }
    }
}
